﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.BLL.Base;
using iSun.Common;
using iSun.HR.Entity;


//==================================================================
// Copyright (C) 2011 iSun Studio 软件工作室
// 作 者：江怀玉
// Email: 
// 日 期：2012/3/28
// 描 述：员工基本信息业务处理
// 版 本：1.0.0.0    
//    
//==================================================================

namespace iSun.BLL
{
    public class BEmployee : BaseBussnies
    {
        public Employee Employee
        {
            get
            {
                Employee employee = this.entity as Employee;
                if (employee == null)
                    throw new ArgumentNullException("this.entity---employee");
                return employee;
            }
        }

        /// <summary>
        /// 是否存在己用的身份证
        /// </summary>
        /// <param name="IDCard">身份证号</param>
        /// <returns></returns>
        public bool IsExist(string IDCard)
        {
            return GetEmployeeByIDCard(IDCard)==null;
        }

        /// <summary>
        /// 通过身份证
        /// </summary>
        /// <param name="IDCard"></param>
        /// <returns></returns>
        public Employee GetEmployeeByIDCard(string IDCard)
        {
            return GetEntityByCondition<Employee>("IDCardNo='" + IDCard + "'");
        }

        /// <summary>
        /// 通过编码 
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public Employee GetEmployeeByCode(string Code)
        {
            return GetEntityByCondition<Employee>("EmployeeCode='" + Code + "'");
        }

        /// <summary>
        /// ovt
        /// </summary>
        /// <returns></returns>
        public override string CheckData()
        {
            IList<string> list = new List<string>();     

            string result = string.Empty;
            if (false == Employee.CName.IsCharacter(20))
            {
                result = "姓名必须是汉字,并且字数不能超过20个;";
                list.Add(result);                
            }

            if (false == Employee.IDCardNO.IsIDCardNO())
            {
                result = "身份证必须是15位或者18位,并且由数字或者数字和x字符组成;";
                list.Add(result);
            }
            else
            {
                if (true == string.IsNullOrEmpty(Employee.ID.ToString()))
                {
                    Employee emp = this.GetEntityByCondition<Employee>("IDCardNO='" + Employee.IDCardNO + "'");
                    if (emp != null)
                    {
                        result = "身份证号已经存在,表示此员工是返聘员工,无须录入基本资料;";
                        list.Add(result);
                    }
                }
                else
                {
                    //检查当前Employee的IDCardNO是否被其它人使用
                    if (this.GetEntitysByCondition<Employee>("ID!='" + Employee.ID.ToString() + "' and IDCardNO='" + Employee.IDCardNO + "'") != null)
                    {
                        result = "身份证号已经存在;";
                        list.Add(result);
                    }
                }
            }
            
            if (false == Employee.PhoneNo.IsMobilePhoneNO())
            {
                result = "手机号码由11位数字组成;";
                list.Add(result);
            }

            if (true == string.IsNullOrEmpty(Employee.Email))
            {
                if (false == Employee.Email.IsEmail())
                {
                    result = "Email格式类似为:name@domain.com;";
                    list.Add(result);
                }
            }

            if (false == Employee.EmployeeCode.HasValue())
            {
                result = "必须要填写员工编号;";
                list.Add(result);
            }

            if (list.Count > 0)
            {
                return Common.Globals.getJSON<string>(list);
            }
            else
            {
                return "";
            }
        }
    }
}
